#include "m0wig.h"
#include "ui_m0wig.h"

#define TOPIC_CTL_DOWN      "/app/control/down"

M0Wig::M0Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::M0Wig)
{
    ui->setupUi(this);

    connect(GlobalVal::mqtt,SIGNAL(dataUpSignal(QByteArray)),this,SLOT(handleMqttMessage(QByteArray)));

    connect(ui->pushButtonLED,SIGNAL(clicked()),this,SLOT(handleLightSlot()));
}

M0Wig::~M0Wig()
{
    delete ui;
}

void M0Wig::handleMqttMessage(const QByteArray &message)
{
    qDebug() << " MQTT 接收 " ;

    qDebug() << "msg" << message;

    //QByteArray byteArray = message.toUtf8();
    QJsonDocument doc = QJsonDocument::fromJson(message);
    QJsonObject obj = doc.object();

    int type = obj["type"].toInt();
    int result = obj["result"].toInt();

    qDebug() << "type" << type << " result" <<result ;

    //QJsonObject stm32 = obj["stm32"].toObject();
    QJsonArray dataArray = obj["data"].toArray();


    int led_value;
    double hum_value,tmp_value,bat_value;

    for (int i = 0; i < dataArray.size(); i++)
    {
        QJsonObject dataObj = dataArray[i].toObject();
        int key = dataObj["key"].toInt();

        qDebug() << "key" <<key;
        QString val = dataObj["val"].toString();

        switch (key)
        {
        case 307:
            led_value = val.toInt();
            qDebug() << " led_value" <<led_value;
            if(led_value == 0)
            {
                ui->pushButtonLED->setText("Open");
            }
            else
            {
                ui->pushButtonLED->setText("Close");
            }
            break;
        case 308:
            bat_value = val.toDouble();
            qDebug() << " bat_value" <<bat_value;
            ui->lcdNumberBat->display(bat_value);
            break;
        case 309:
            tmp_value = val.toDouble();
            ui->lcdNumberTmp->display(tmp_value);
            break;
        case 310:
            hum_value = val.toDouble();
            ui->lcdNumberHum->display(hum_value);
            break;
        default:
            break;
        }
    }

}

void M0Wig::handleLightSlot()
{
    int value;
    if(ui->pushButtonLED->text() == "Open")
    {
        qDebug() <<" open";
        ui->pushButtonLED->setText("Close");
        value = 1;
    }
    else if(ui->pushButtonLED->text() == "Close")
    {
        qDebug() <<"close";
        ui->pushButtonLED->setText("Open");
        value = 0;
    }
    else
    {

    }

    QJsonObject json;
    json["type"] = 2;
    QJsonObject data;
    data["key"] = 307;
    data["value"] = QString::number(value);
    json["data"] = data;
    QJsonDocument doc(json);
    QByteArray payload = doc.toJson();
    QMQTT::Message message;
    //qDebug() << payload ;
    message.setTopic(TOPIC_CTL_DOWN); //设置主题
    message.setPayload(payload);//负载
    quint16 messageId = GlobalVal::mqtt->publish(message);
    // qDebug() << "messageId " << messageId ;
    if(messageId == 0)
    {
        QMessageBox::warning(this,"warn","设置失败");
    }
    else
    {
        if(value == 0)
            QMessageBox::information(this,"info","关灯成功");
        else
            QMessageBox::information(this,"info","开灯成功");
    }
}

